﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class ABPOSController : ITXController
    {
        [ChildActionOnly]
        public ActionResult PAB05RZ(string returnUrl)
        {
            ModelState.Clear();
            AUTOCHK();
            ViewBag.ReturnUrl = returnUrl;
            ViewBag.IsERKReady = MABF.VWBB.IsERKReady;
            ViewBag.IsSTKNRReady = MABF.IsSTKNRReady;
            ViewBag.IsAMRunReady = MABF.IsAMRunReady;
            ViewBag.IsVAFReady = MABF.IsVAFReady;
            ViewBag.IsVAReady = MABF.IsVAReady;
            ViewBag.IsWFReady = MABF.IsWFReady;
            ViewBag.IsWSReady = MABF.IsWSReady;
            ViewBag.IsPDCReady = MABF.IsPDCReady;
            ViewBag.IsABPReady = MABF.IsABPReady;
            ViewBag.IsEXORDReady = MABF.IsEXORDReady;
            ViewBag.IsAMReady = MABF.IsAMReady; 
            return PartialView();
          
        }

        [ChildActionOnly]
        public ActionResult MAB05RZ(string returnUrl)
        {
            ModelState.Clear();  
            ViewBag.ReturnUrl = returnUrl;
            return PartialView(MABF.MABP);
        }
        
        public void AUTOCHK()
        {
            if (MABF.IsEXORDReady)
                return;
            DataRow dwABP = null;
            dwData = VHP.ABP.TBMain.NewRow();
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPAUFKRS"] = MABF.EXORD.Substring(1, 2);
            dwData["ABPAUFLFD"] = MABF.EXORD.Substring(3, 6);
            dwData["ABPAUFPOS"] = MABF.EXORD.Substring(9, 3);
            SResult = VHP.ABP.QABP01(dwData);
            foreach(DataRow row in SResult.Tables[0].Rows)
            {
                dwABP = row;
                SetUpABP(dwABP);
                return;
            }
            PM["ARTST"] = MABF.WFART.Substring(0, 1);
            PM["KOLL"] = MABF.VAF.VAFKOLL;
            PM["ART"] = MABF.WFART.Substring(1, 20);
            PM["FARBE"] = MABF.WFART.Substring(23, 10);
            PM["DES"] = MABF.WFART.Substring(1 + 20 + 2 + 10 * 4 + 7, 20);
            PM["RUNID"] = this.RUNID;
            PM["KRS"] = MABF.EXORD.Substring(1, 2);
            PM["AUFART"] = "VA";
            PM["ORDERNUM"] = MABF.EXORD.Substring(1, 11);
            PM["LFD"] = MABF.EXORD.Substring(3, 6);
            PM["RELOADABK"] = false;
            string ABAUF = VHP.VABK.NEWABK(PM);
            LFD = ABAUF.Substring(2, 6);
            POS = MABF.EXORD.Substring(9, 3);
            PM["ORDERNUM"] = ABAUF.Substring(0, 2) + LFD + POS + "VA ";
            PM["COLOR"] = "VA ";
            PM["AUFTYP"] = "V";
            PM["ARTSTAGE"] = "Z";
            //PM["VKAUF"] = pm["ORDERNUM"];
            PM["VKPAUF"] = KRS + LFD + POS;
            PM["VKPQTY"] = 0;//
            PM["VKAUF"] = "                  ";//
            PM["UNDES"] = "HZ000001";
            PM["DELDATE"] = VHP.VPR.date;
            PM["STARTDATE"] = VHP.VPR.date;

            VHP.VPR.NEWPLAN(PM);
            bool IsABPCreated = false;
            PM["ORDER"] = PM["ORDERNUM"];
            PM["WAKZ"] = MABF.WFART.Substring(0, 1);

            PM["VAR"] = "  ";

            PM["BESCHICH"] = MABF.WFART.Substring(33, 10);
            PM["DESS"] = MABF.WFART.Substring(43, 10);
            PM["COLORIT"] = MABF.WFART.Substring(53, 10);
            PM["GROE"] = MABF.WFART.Substring(63, 7);
            PM["MAKE2ORD"] = false;
            PM["IsDI"] = false;
            dwABP= VHP.ABP.RZABPNEW(PM, ref IsABPCreated);

            SetUpABP(dwABP);
        }
        private void SetUpABP(DataRow dwABP)
        {
            MABF.MABP = new ABPOSModel();
            MABF.MABP.dwABP = dwABP;
            MABF.KRS = dwABP["ABPAUFKRS"].ToString();
            MABF.LFD = dwABP["ABPAUFLFD"].ToString();
            MABF.POS = dwABP["ABPAUFPOS"].ToString();
            MABF.ABPAUF = dwABP["ABPAUFKRS"].ToString() + dwABP["ABPAUFLFD"].ToString() + dwABP["ABPAUFPOS"].ToString();
            MABF.MABP.ABPAUF = dwABP["ABPAUFKRS"].ToString() + dwABP["ABPAUFLFD"].ToString() + dwABP["ABPAUFPOS"].ToString();
            MABF.IsEXORDReady = true;
        }
    }
}
